﻿using UnityEngine;
using System.Collections;
using System.Text.RegularExpressions;

public partial class LoginScene : Scene
{
	#region - G-Start Version
	public void OnClick_GStar_Login()
	{
		NetworkManager.Instance.C2S_GSTAR_JOIN((responseData) => {
			UserData.Instance.userModel = responseData.data;
            UserData.Instance.isExistRaidBoss = false;
            WorldSceneManager_GStar.sentResearchAreaId = 0;
			Invoke("GetServerTimeStamp", 0.5f);
		}, OnErrorCallback);
	}
	#endregion

	public void OnClick_Login ()
	{
		string _id = loginIdInput.label.text;

		if (ValidateLoginCondition(_id) == false)
			return;

		NetworkManager.Instance.C2S_LOGIN(AuthPlatformID.None, _id, string.Empty, (responseData) => {
			UserData.Instance.userModel = responseData.data;
            Invoke("GetServerTimeStamp", 0.5f);
		}, OnErrorCallback);
	}

    void GetServerTimeStamp()
    {
        NetworkManager.Instance.C2S_GET_SERVERTIME(ResponseGetServerTime);
    }

    void GetAchievementList()
    {
        NetworkManager.Instance.C2S_GET_ACHIEVEMENT_LIST((result) =>
        {
            UserData.Instance.NorewardGetAchieveCount = result.data.Count;
        }, 4);
    }

	public void ResponseGetServerTime(ResponseModel<double> result)
	{
		UserData.Instance.serverTimeOffset = Constants.ConvertToUnixTimestamp(System.DateTime.Now) - result.data;
		UserData.Instance.serverTimeOffsetSpan = System.DateTime.Now - Constants.ConvertFromUnixTimestamp(result.data);
		SceneController.LoadLevel("Main");
	}

	bool ValidateLoginCondition(string _id)
	{
		if (_id.Trim().Length <= 0 || string.Compare(_id.Trim(), initialLoginIdText) == 0)
		{
			PopupUtility.OneButtonErrorPopup(Localization.Get("uiStartCaution3"));
			return false;
		}

		if (_id.Contains(" "))
		{
			PopupUtility.OneButtonErrorPopup(Localization.Get("uiPopupStartCaution4"));
			return false;
		}

		if (_id.Length > 20)
		{
			PopupUtility.OneButtonErrorPopup(Localization.Get("uiPopupStartCaution5"));
			return false;
		}

		return true;
	}

	bool ValidateJoinCondition(string _id, string _name)
	{
		// id 체크를 위한 정규표현식
		Regex idRegex = new Regex(@"[^a-zA-Z0-9]");
		Match idMatch = idRegex.Match(_id.Trim());

		// name 체크를 위한 정규표현식
		Regex nameRegex = new Regex(@"[^ㄱ-ㅎ가-힣a-zA-Z0-9]");
		Match nameMatch = nameRegex.Match(_name.Trim());

		// id, name의 특수문자 체크
		if (idMatch.Success || nameMatch.Success)
		{
			PopupUtility.OneButtonErrorPopup(Localization.Get("uiPopupStartCaution9"));
			return false;
		}
		
		if (_id.Trim().Length <= 0 || _name.Trim().Length <= 0 || string.Compare(_id.Trim(), initialJoinIdText) == 0 || string.Compare(_name.Trim(), initialJoinNameText) == 0)
		{
			PopupUtility.OneButtonErrorPopup(Localization.Get("uiStartCaution2"));
			return false;
		}

		if (_id.Contains(" "))
		{
			PopupUtility.OneButtonErrorPopup(Localization.Get("uiPopupStartCaution4"));
			return false;
		}
		
		if (_id.Length > 20)
		{
			PopupUtility.OneButtonErrorPopup(Localization.Get("uiPopupStartCaution5"));
			return false;
		}

		if (_name.Length > 10)
		{
			PopupUtility.OneButtonErrorPopup(Localization.Get("uiPopupStartCaution6"));
			return false;
		}

		return true;
	}

	public void OnClick_Join()
	{
		string _id = joinIdInput.label.text.Trim();
		string _name = joinNameInput.label.text.Trim();
		
		if (ValidateJoinCondition(_id, _name) == false)
			return;

		NetworkManager.Instance.C2S_USER_JOIN(AuthPlatformID.None, _id, string.Empty, _name, (responseData) => {
			Debug.Log ("OnClick_Join Callback");
			StartCoroutine( SwitchJoinToLoginCoroutine() );
		}, OnErrorCallback);
	}



	public void OnClick_SwitchJoinToLogin() { StartCoroutine( SwitchJoinToLoginCoroutine() ); }
	IEnumerator SwitchJoinToLoginCoroutine()
	{
		currentMode = LoginMode.Login;

		joinAnimation.Play(isLoginAreaUp ? "Login_join_up_out" : "Login_join_out");
		yield return new WaitForSeconds(0.5f);
		loginAnimation.Play("Login_login_in");

		isLoginAreaUp = false;
		joinIdInput.value = string.Empty;
		joinIdInput.label.text = initialJoinIdText;
		joinNameInput.value = string.Empty;
		joinNameInput.label.text = initialJoinNameText;
	}

	
	public void OnClick_SwitchLoginToJoin() { StartCoroutine( SwitchLoginToJoinCoroutine() ); }
	IEnumerator SwitchLoginToJoinCoroutine()
	{
		currentMode = LoginMode.Join;

		loginAnimation.Play(isLoginAreaUp ? "Login_login_up_out" : "Login_login_out");
		yield return new WaitForSeconds(0.5f);
		joinAnimation.Play("Login_join_in");

		isLoginAreaUp = false;
		loginIdInput.value = string.Empty;
		loginIdInput.label.text = initialLoginIdText;
	}

	void OnErrorCallback(ResponseModel<UserModel> responseData)
	{
		PopupUtility.OneButtonErrorPopup(responseData.ecode);
	}
}